#!/bin/sh
#
# Script to install the SQL Schema
# --------------------------------------------------------------
# Read the list of file names from the schema-install.fifo file 
# and install the scme using a shell script or a perl script
#
#---------------------------------------------------------------

CAT="/bin/cat"
SQL_FILES_DIR="/opt/sql"

fromdos $SQL_FILES_DIR/schema-install.fifo
find $SQL_FILES_DIR/ -name "*.sql" -exec fromdos -d {} \;

service mysql restart

sleep 5

for LINE in `cat $SQL_FILES_DIR/schema-install.fifo`
do
        LINE=$(echo "$LINE" | sed -e 's/[[:space:]]*//')
        SQL_FILE=$SQL_FILES_DIR$LINE
        echo $SQL_FILE
        case "$LINE" in
        *.tar.gz)
                tar zxf $SQL_FILE --to-stdout | /usr/bin/mysql --f --user=root --password=xAVa6Aze
                if [ $? -ne 0 ]; then
                        echo "Failed to install the db schema from $LINE"
                fi
                ;;
        *)
                fromdos $SQL_FILE
                $CAT $SQL_FILE | /usr/bin/mysql --f --user=root --password=xAVa6Aze
                if [ $? -ne 0 ]; then
                        echo "Failed to install the db schema from $LINE"
                fi
                ;;
        esac
       
done

/usr/bin/mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql --user=root --password=xAVa6Aze mysql

service mongodb restart

sleep 60

/usr/bin/mongorestore -d monitoring_db $SQL_FILES_DIR/schema/mongo/

/usr/bin/mongo instances_db --eval "printjson(db.dropDatabase())"
/usr/bin/mongo monitoring_db --eval "printjson(db.dropDatabase())"
/usr/bin/mongo instances_db --eval "printjson(db.instances_data.ensureIndex({deviceId:1, currentState:1}))"
/usr/bin/mongo instances_db --eval "printjson(db.instances_data.ensureIndex({deviceId:1, monitorId:1}))"
/usr/bin/mongo instances_db --eval "printjson(db.instances_data.ensureIndex({serviceGroupId:1, monitorId:1}))"
/usr/bin/mongo monitoring_db --eval "printjson(db.monitors_data.ensureIndex({path:1}))"
/usr/bin/mongo monitoring_db --eval "printjson(db.calldetails.ensureIndex({globalCallIDCallManagerId:1, globalCallIDCallId:1, saId:1}))"
/usr/bin/mongo monitoring_db --eval "printjson(db.calldetails.ensureIndex({clientId:1, ucmIP:1, dateTimeDisconnect:1}))"
/usr/bin/mongo monitoring_db --eval "printjson(db.calldetails.ensureIndex({clientId:1, dateTimeDisconnect:1}))"
/usr/bin/mongo monitoring_db --eval "printjson(db.calldetails.ensureIndex({clientId:1}))"
/usr/bin/mongo monitoring_db --eval "printjson(db.calldetails.ensureIndex({clientId:1, dateTimeDisconnect:1, duration:1}))"
/usr/bin/mongo monitoring_db --eval "printjson(db.calldetails.ensureIndex({clientId:1, dateTimeDisconnect:1,avgMOSLQK:1,duration:1}))"
/usr/bin/mongo monitoring_db --eval "printjson(db.calldetails.ensureIndex({clientId:1, origMediaCapPayloadCapabilityLabel:1}))"
/usr/bin/mongo monitoring_db --eval "printjson(db.calldetails.ensureIndex({clientId:1, origMediaCapPayloadCapabilityLabel:1, origMediaCapBandwidth:1}))"

/usr/bin/mongorestore -d monitoring_db $SQL_FILES_DIR/schema/mongo/

echo "Installing cassandra schema..."
service cassandra restart
sleep 30
service cassandra status

for LINE in `cat $SQL_FILES_DIR/cassandra-schema-install.fifo`
do
    LINE=$(echo "$LINE" | sed -e 's/[[:space:]]*//')
    SQL_FILE=$SQL_FILES_DIR$LINE
    echo $SQL_FILE
    
    fromdos $SQL_FILE
    /usr/bin/cqlsh -ucassandra -pcassandra -f $SQL_FILE
    if [ $? -ne 0 ]; then
    	echo "Failed to install the Cassandra schema from $LINE"
    fi
done

	